import { reqGetGoodInfo, reqAddShopCart } from '@/api'

const state = {
  goodInfo: {}
}
const mutations = {
  GETGOODINFO(state, goodInfo) {
    state.goodInfo = goodInfo
  }
}
const actions = {
  async getGoodInfo({ commit }, skuId) {
    const res = await reqGetGoodInfo(skuId)
    if (res.code == 200) {
      commit('GETGOODINFO', res.data)
    }
  },
  async addOrUpdataShopCart({ commit }, { skuId, skuNum }) {
    let res = await reqAddShopCart(skuId, skuNum)
    if (res.code == 200) {
      return ''
    } else {
      return Promise.reject(Error('添加购物车失败!'))
    }
  }
}
const getters = {
  categoryView(state) {
    return state.goodInfo.categoryView || {}
  },
  skuInfo(state) {
    return state.goodInfo.skuInfo || {}
  },
  spuSaleAttrList(state) {
    return state.goodInfo.spuSaleAttrList || []
  }
}

export default {
  state,
  mutations,
  actions,
  getters
}
